//
// Created by fairy on 2025/7/8.
//
#include <rcc.hpp>

extern "C"
{
    HAL_StatusTypeDef HAL_InitTick(const uint32_t TickPriority)
    {
        using namespace hal;

        __HAL_RCC_TIM7_CLK_ENABLE();
        rcc::getTIMBaseHandler()->Instance = TIM7;
        rcc::getTIMBaseHandler()->Init.Prescaler = 20;
        rcc::getTIMBaseHandler()->Init.Period = 3999;
        rcc::getTIMBaseHandler()->Init.ClockDivision = 0;
        rcc::getTIMBaseHandler()->Init.CounterMode = TIM_COUNTERMODE_UP;
        rcc::getTIMBaseHandler()->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

        HAL_StatusTypeDef status = HAL_TIM_Base_Init(rcc::getTIMBaseHandler());
        if (status == HAL_OK)
        {
            /* Start the TIM time Base generation in interrupt mode */
            status = HAL_TIM_Base_Start_IT(rcc::getTIMBaseHandler());
            if (status == HAL_OK)
            {
                /* Enable the TIM7 global Interrupt */
                HAL_NVIC_EnableIRQ(TIM7_IRQn);
                /* Configure the SysTick IRQ priority */
                if (TickPriority < (1UL << __NVIC_PRIO_BITS))
                {
                    /* Configure the TIM IRQ priority */
                    HAL_NVIC_SetPriority(TIM7_IRQn, TickPriority, 0U);
                    uwTickPrio = TickPriority;
                } else
                {
                    status = HAL_ERROR;
                }
            }
        }

        /* Return function status */
        return status;
    }


    inline void HAL_SuspendTick(void)
    {
        using namespace hal;
        __HAL_TIM_DISABLE_IT(rcc::getTIMBaseHandler(), TIM_IT_UPDATE);
    }

    inline void HAL_ResumeTick(void)
    {
        using namespace hal;
        __HAL_TIM_ENABLE_IT(rcc::getTIMBaseHandler(), TIM_IT_UPDATE);
    }


}